package com.guodu.dao.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.MaterialKucunDao;
import com.guodu.entity.MaterialKucun;
import com.guodu.entity.MaterialkucunStatus;
import com.guodu.entity.Units;
import com.guodu.vo.MaterialKucunQueryVo;

@Repository("materialKucunDao")
public class MaterialKucunDaoImpl extends BaseDao implements MaterialKucunDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialKucun> findByVo(MaterialKucunQueryVo materialKucunQueryVo,Units units,
			int curPage, int pageSize) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from MaterialKucun m where 1=1");
		if(materialKucunQueryVo!=null){
			if(materialKucunQueryVo.getMaterialType()!=null&&materialKucunQueryVo.getMaterialType().trim().length()>0){
				hql.append(" and m.materialType = '"+materialKucunQueryVo.getMaterialType()+"'");
			}
			if(materialKucunQueryVo.getDrawingnum()!=null&&materialKucunQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialKucunQueryVo.getDrawingnum()+"%'");
			}
			if(materialKucunQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialKucunQueryVo.getFunitsId()+")");
			}
			if(materialKucunQueryVo.getMaterialCode()!=null&&materialKucunQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialKucunQueryVo.getMaterialCode()+"%'");
			}
			if(materialKucunQueryVo.getMaterialName()!=null&&materialKucunQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialKucunQueryVo.getMaterialName()+"%'");
			}
			if(materialKucunQueryVo.getKcBeginDate()!=null&&materialKucunQueryVo.getKcBeginDate().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') >= TO_DATE('"+materialKucunQueryVo.getKcBeginDate()+"','yyyy-mm')");
			}
			if(materialKucunQueryVo.getKcEndDate()!=null&&materialKucunQueryVo.getKcEndDate().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') <= TO_DATE('"+materialKucunQueryVo.getKcEndDate()+"','yyyy-mm')");
			}
			if(materialKucunQueryVo.getSpecifications()!=null&&materialKucunQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specifications like '%"+materialKucunQueryVo.getSpecifications()+"%'");
			}
			if(materialKucunQueryVo.getTexture()!=null&&materialKucunQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialKucunQueryVo.getTexture()+"%'");
			}
			if(materialKucunQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialKucunQueryVo.getUnitsId());
			}
			if(materialKucunQueryVo.getCreateBeginTime()!=null&&materialKucunQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialKucunQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialKucunQueryVo.getCreateEndTime()!=null&&materialKucunQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialKucunQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialKucunQueryVo.getStatus()!=null&&materialKucunQueryVo.getStatus().trim().length()>0&&!materialKucunQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialKucunQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}
		hql.append(" order by kcId desc");
		return super.findPages(hql.toString(), curPage, pageSize);
	}

	@Override
	public int findSizeByVo(
			MaterialKucunQueryVo materialKucunQueryVo,Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select count(*) from MaterialKucun m where 1=1");
		if(materialKucunQueryVo!=null){
			if(materialKucunQueryVo.getMaterialType()!=null&&materialKucunQueryVo.getMaterialType().trim().length()>0){
				hql.append(" and m.materialType = '"+materialKucunQueryVo.getMaterialType()+"'");
			}
			if(materialKucunQueryVo.getDrawingnum()!=null&&materialKucunQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialKucunQueryVo.getDrawingnum()+"%'");
			}
			if(materialKucunQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialKucunQueryVo.getFunitsId()+")");
			}
			if(materialKucunQueryVo.getMaterialCode()!=null&&materialKucunQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialKucunQueryVo.getMaterialCode()+"%'");
			}
			if(materialKucunQueryVo.getMaterialName()!=null&&materialKucunQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialKucunQueryVo.getMaterialName()+"%'");
			}
			if(materialKucunQueryVo.getKcBeginDate()!=null&&materialKucunQueryVo.getKcBeginDate().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') >= TO_DATE('"+materialKucunQueryVo.getKcBeginDate()+"','yyyy-mm')");
			}
			if(materialKucunQueryVo.getKcEndDate()!=null&&materialKucunQueryVo.getKcEndDate().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') <= TO_DATE('"+materialKucunQueryVo.getKcEndDate()+"','yyyy-mm')");
			}
			if(materialKucunQueryVo.getSpecifications()!=null&&materialKucunQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specifications like '%"+materialKucunQueryVo.getSpecifications()+"%'");
			}
			if(materialKucunQueryVo.getTexture()!=null&&materialKucunQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialKucunQueryVo.getTexture()+"%'");
			}
			if(materialKucunQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialKucunQueryVo.getUnitsId());
			}
			if(materialKucunQueryVo.getCreateBeginTime()!=null&&materialKucunQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialKucunQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialKucunQueryVo.getCreateEndTime()!=null&&materialKucunQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialKucunQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialKucunQueryVo.getStatus()!=null&&materialKucunQueryVo.getStatus().trim().length()>0&&!materialKucunQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialKucunQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}
		return super.findInt(hql.toString());
	}

	@Override
	public MaterialKucun findById(String id) {
		// TODO Auto-generated method stub
		return (MaterialKucun) super.findById(MaterialKucun.class, id);
	}

	@Override
	public void del(String id) {
		// TODO Auto-generated method stub
		super.delete(MaterialKucun.class, id);
	}

	@Override
	public void delByIds(String[] ids) {
		// TODO Auto-generated method stub
		super.deleteAllById(MaterialKucun.class, ids);
	}

	@Override
	public void save(MaterialKucun entity) {
		// TODO Auto-generated method stub
		entity.setKcId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		super.save(entity);
	}

	@Override
	public void examine(String ids, Long status) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update MaterialKucun set status =");
		hql.append(status);
		hql.append(" where kcId in (");
		hql.append(ids);
		hql.append(")");
		super.executeUpdate(hql.toString());
	}

	@Override
	public void update(MaterialKucun entity) {
		// TODO Auto-generated method stub
		super.update(entity);
	}

	@Override
	public void changeStatus(List<MaterialkucunStatus> entities) {
		// TODO Auto-generated method stub
		super.saveAll(entities);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialkucunStatus> findStatus(String id) {
		// TODO Auto-generated method stub
		return super.findByProperty(MaterialkucunStatus.class, "materialId", id);
	}

	@Override
	public List<MaterialKucun> findAllByVo(
			MaterialKucunQueryVo materialKucunQueryVo, Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from MaterialKucun m where 1=1");
		if(materialKucunQueryVo!=null){
			if(materialKucunQueryVo.getMaterialType()!=null&&materialKucunQueryVo.getMaterialType().trim().length()>0){
				hql.append(" and m.materialType = '"+materialKucunQueryVo.getMaterialType()+"'");
			}
			if(materialKucunQueryVo.getDrawingnum()!=null&&materialKucunQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialKucunQueryVo.getDrawingnum()+"%'");
			}
			if(materialKucunQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialKucunQueryVo.getFunitsId()+")");
			}
			if(materialKucunQueryVo.getMaterialCode()!=null&&materialKucunQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialKucunQueryVo.getMaterialCode()+"%'");
			}
			if(materialKucunQueryVo.getMaterialName()!=null&&materialKucunQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialKucunQueryVo.getMaterialName()+"%'");
			}
			if(materialKucunQueryVo.getKcBeginDate()!=null&&materialKucunQueryVo.getKcBeginDate().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') >= TO_DATE('"+materialKucunQueryVo.getKcBeginDate()+"','yyyy-mm')");
			}
			if(materialKucunQueryVo.getKcEndDate()!=null&&materialKucunQueryVo.getKcEndDate().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') <= TO_DATE('"+materialKucunQueryVo.getKcEndDate()+"','yyyy-mm')");
			}
			if(materialKucunQueryVo.getSpecifications()!=null&&materialKucunQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specifications like '%"+materialKucunQueryVo.getSpecifications()+"%'");
			}
			if(materialKucunQueryVo.getTexture()!=null&&materialKucunQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialKucunQueryVo.getTexture()+"%'");
			}
			if(materialKucunQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialKucunQueryVo.getUnitsId());
			}
			if(materialKucunQueryVo.getCreateBeginTime()!=null&&materialKucunQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialKucunQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialKucunQueryVo.getCreateEndTime()!=null&&materialKucunQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialKucunQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialKucunQueryVo.getStatus()!=null&&materialKucunQueryVo.getStatus().trim().length()>0&&!materialKucunQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialKucunQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=1");
		}
		hql.append(" order by createTime desc");
		return super.find(hql.toString());
	}
	
	@Override
	public BigDecimal findAllMoney(MaterialKucunQueryVo materialKucunQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select sum(money) from MaterialKucun m where 1=1");
		if(materialKucunQueryVo!=null){
			if(materialKucunQueryVo.getMaterialType()!=null&&materialKucunQueryVo.getMaterialType().trim().length()>0){
				hql.append(" and m.materialType = '"+materialKucunQueryVo.getMaterialType()+"'");
			}
			if(materialKucunQueryVo.getDrawingnum()!=null&&materialKucunQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialKucunQueryVo.getDrawingnum()+"%'");
			}
			if(materialKucunQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialKucunQueryVo.getFunitsId()+")");
			}
			if(materialKucunQueryVo.getMaterialCode()!=null&&materialKucunQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialKucunQueryVo.getMaterialCode()+"%'");
			}
			if(materialKucunQueryVo.getMaterialName()!=null&&materialKucunQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialKucunQueryVo.getMaterialName()+"%'");
			}
			if(materialKucunQueryVo.getKcBeginDate()!=null&&materialKucunQueryVo.getKcBeginDate().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') >= TO_DATE('"+materialKucunQueryVo.getKcBeginDate()+"','yyyy-mm')");
			}
			if(materialKucunQueryVo.getKcEndDate()!=null&&materialKucunQueryVo.getKcEndDate().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') <= TO_DATE('"+materialKucunQueryVo.getKcEndDate()+"','yyyy-mm')");
			}
			if(materialKucunQueryVo.getSpecifications()!=null&&materialKucunQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specifications like '%"+materialKucunQueryVo.getSpecifications()+"%'");
			}
			if(materialKucunQueryVo.getTexture()!=null&&materialKucunQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialKucunQueryVo.getTexture()+"%'");
			}
			if(materialKucunQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialKucunQueryVo.getUnitsId());
			}
			if(materialKucunQueryVo.getCreateBeginTime()!=null&&materialKucunQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialKucunQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialKucunQueryVo.getCreateEndTime()!=null&&materialKucunQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialKucunQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialKucunQueryVo.getStatus()!=null&&materialKucunQueryVo.getStatus().trim().length()>0&&!materialKucunQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialKucunQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}
		return super.findDouble(hql.toString());
	}

	@Override
	public boolean examinAll(MaterialKucunQueryVo materialKucunQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update MaterialKucun m set status=1  where status=0");
		if(materialKucunQueryVo!=null){
			if(materialKucunQueryVo.getMaterialType()!=null&&materialKucunQueryVo.getMaterialType().trim().length()>0){
				hql.append(" and m.materialType = '"+materialKucunQueryVo.getMaterialType()+"'");
			}
			if(materialKucunQueryVo.getDrawingnum()!=null&&materialKucunQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialKucunQueryVo.getDrawingnum()+"%'");
			}
			if(materialKucunQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialKucunQueryVo.getFunitsId()+")");
			}
			if(materialKucunQueryVo.getMaterialCode()!=null&&materialKucunQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialKucunQueryVo.getMaterialCode()+"%'");
			}
			if(materialKucunQueryVo.getMaterialName()!=null&&materialKucunQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialKucunQueryVo.getMaterialName()+"%'");
			}
			if(materialKucunQueryVo.getKcBeginDate()!=null&&materialKucunQueryVo.getKcBeginDate().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') >= TO_DATE('"+materialKucunQueryVo.getKcBeginDate()+"','yyyy-mm')");
			}
			if(materialKucunQueryVo.getKcEndDate()!=null&&materialKucunQueryVo.getKcEndDate().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') <= TO_DATE('"+materialKucunQueryVo.getKcEndDate()+"','yyyy-mm')");
			}
			if(materialKucunQueryVo.getSpecifications()!=null&&materialKucunQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specifications like '%"+materialKucunQueryVo.getSpecifications()+"%'");
			}
			if(materialKucunQueryVo.getTexture()!=null&&materialKucunQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialKucunQueryVo.getTexture()+"%'");
			}
			if(materialKucunQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialKucunQueryVo.getUnitsId());
			}
			if(materialKucunQueryVo.getCreateBeginTime()!=null&&materialKucunQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialKucunQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialKucunQueryVo.getCreateEndTime()!=null&&materialKucunQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialKucunQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialKucunQueryVo.getStatus()!=null&&materialKucunQueryVo.getStatus().trim().length()>0&&!materialKucunQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialKucunQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}
		hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialKucunQueryVo.getFunitsId()+")");
		super.executeUpdate(hql.toString());
		return true;
	}
	@Override
	public boolean huituiAll(MaterialKucunQueryVo materialKucunQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update MaterialKucun m set status=2  where status=0");
		if(materialKucunQueryVo!=null){
			if(materialKucunQueryVo.getMaterialType()!=null&&materialKucunQueryVo.getMaterialType().trim().length()>0){
				hql.append(" and m.materialType = '"+materialKucunQueryVo.getMaterialType()+"'");
			}
			if(materialKucunQueryVo.getDrawingnum()!=null&&materialKucunQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialKucunQueryVo.getDrawingnum()+"%'");
			}
			if(materialKucunQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialKucunQueryVo.getFunitsId()+")");
			}
			if(materialKucunQueryVo.getMaterialCode()!=null&&materialKucunQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialKucunQueryVo.getMaterialCode()+"%'");
			}
			if(materialKucunQueryVo.getMaterialName()!=null&&materialKucunQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialKucunQueryVo.getMaterialName()+"%'");
			}
			if(materialKucunQueryVo.getKcBeginDate()!=null&&materialKucunQueryVo.getKcBeginDate().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') >= TO_DATE('"+materialKucunQueryVo.getKcBeginDate()+"','yyyy-mm')");
			}
			if(materialKucunQueryVo.getKcEndDate()!=null&&materialKucunQueryVo.getKcEndDate().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') <= TO_DATE('"+materialKucunQueryVo.getKcEndDate()+"','yyyy-mm')");
			}
			if(materialKucunQueryVo.getSpecifications()!=null&&materialKucunQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specifications like '%"+materialKucunQueryVo.getSpecifications()+"%'");
			}
			if(materialKucunQueryVo.getTexture()!=null&&materialKucunQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialKucunQueryVo.getTexture()+"%'");
			}
			if(materialKucunQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialKucunQueryVo.getUnitsId());
			}
			if(materialKucunQueryVo.getCreateBeginTime()!=null&&materialKucunQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialKucunQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialKucunQueryVo.getCreateEndTime()!=null&&materialKucunQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialKucunQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialKucunQueryVo.getStatus()!=null&&materialKucunQueryVo.getStatus().trim().length()>0&&!materialKucunQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialKucunQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}
		hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+units.getUnitsId()+")");
		super.executeUpdate(hql.toString());
		return true;
	}
	@Override
	public boolean shanchuAll(MaterialKucunQueryVo materialKucunQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("delete from  MaterialKucun m   where (status=0 or status=2)");
		if(materialKucunQueryVo!=null){
			if(materialKucunQueryVo.getMaterialType()!=null&&materialKucunQueryVo.getMaterialType().trim().length()>0){
				hql.append(" and m.materialType = '"+materialKucunQueryVo.getMaterialType()+"'");
			}
			if(materialKucunQueryVo.getDrawingnum()!=null&&materialKucunQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialKucunQueryVo.getDrawingnum()+"%'");
			}
			if(materialKucunQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialKucunQueryVo.getFunitsId()+")");
			}
			if(materialKucunQueryVo.getMaterialCode()!=null&&materialKucunQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialKucunQueryVo.getMaterialCode()+"%'");
			}
			if(materialKucunQueryVo.getMaterialName()!=null&&materialKucunQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialKucunQueryVo.getMaterialName()+"%'");
			}
			if(materialKucunQueryVo.getKcBeginDate()!=null&&materialKucunQueryVo.getKcBeginDate().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') >= TO_DATE('"+materialKucunQueryVo.getKcBeginDate()+"','yyyy-mm')");
			}
			if(materialKucunQueryVo.getKcEndDate()!=null&&materialKucunQueryVo.getKcEndDate().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') <= TO_DATE('"+materialKucunQueryVo.getKcEndDate()+"','yyyy-mm')");
			}
			if(materialKucunQueryVo.getSpecifications()!=null&&materialKucunQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specifications like '%"+materialKucunQueryVo.getSpecifications()+"%'");
			}
			if(materialKucunQueryVo.getTexture()!=null&&materialKucunQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialKucunQueryVo.getTexture()+"%'");
			}
			if(materialKucunQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialKucunQueryVo.getUnitsId());
			}
			if(materialKucunQueryVo.getCreateBeginTime()!=null&&materialKucunQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialKucunQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialKucunQueryVo.getCreateEndTime()!=null&&materialKucunQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialKucunQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialKucunQueryVo.getStatus()!=null&&materialKucunQueryVo.getStatus().trim().length()>0&&!materialKucunQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialKucunQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}else{
			hql.append(" and unitsId="+units.getUnitsId());
		}
		super.executeUpdate(hql.toString());
		return true;
	}
	@Override
	public boolean allBack(MaterialKucunQueryVo materialKucunQueryVo,
			Units units) {
		StringBuffer hql = new StringBuffer("update MaterialKucun m set status=0  where status=1");
		if(materialKucunQueryVo!=null){
			if(materialKucunQueryVo.getMaterialType()!=null&&materialKucunQueryVo.getMaterialType().trim().length()>0){
				hql.append(" and m.materialType = '"+materialKucunQueryVo.getMaterialType()+"'");
			}
			if(materialKucunQueryVo.getDrawingnum()!=null&&materialKucunQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialKucunQueryVo.getDrawingnum()+"%'");
			}
			if(materialKucunQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialKucunQueryVo.getFunitsId()+")");
			}
			if(materialKucunQueryVo.getMaterialCode()!=null&&materialKucunQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialKucunQueryVo.getMaterialCode()+"%'");
			}
			if(materialKucunQueryVo.getMaterialName()!=null&&materialKucunQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialKucunQueryVo.getMaterialName()+"%'");
			}
			if(materialKucunQueryVo.getKcBeginDate()!=null&&materialKucunQueryVo.getKcBeginDate().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') >= TO_DATE('"+materialKucunQueryVo.getKcBeginDate()+"','yyyy-mm')");
			}
			if(materialKucunQueryVo.getKcEndDate()!=null&&materialKucunQueryVo.getKcEndDate().trim().length()>0){
				hql.append(" and TO_DATE(month,'yyyy-mm') <= TO_DATE('"+materialKucunQueryVo.getKcEndDate()+"','yyyy-mm')");
			}
			if(materialKucunQueryVo.getSpecifications()!=null&&materialKucunQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specifications like '%"+materialKucunQueryVo.getSpecifications()+"%'");
			}
			if(materialKucunQueryVo.getTexture()!=null&&materialKucunQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialKucunQueryVo.getTexture()+"%'");
			}
			if(materialKucunQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialKucunQueryVo.getUnitsId());
			}
			if(materialKucunQueryVo.getCreateBeginTime()!=null&&materialKucunQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialKucunQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialKucunQueryVo.getCreateEndTime()!=null&&materialKucunQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialKucunQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialKucunQueryVo.getStatus()!=null&&materialKucunQueryVo.getStatus().trim().length()>0&&!materialKucunQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialKucunQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}
		
		super.executeUpdate(hql.toString());
		return true;
	}


}
